﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using MarketOnlineDTO;
using MySql.Data.MySqlClient;
using System.Collections;


namespace MarketOnlineDAO
{
    public class HoaDonDAO : BaseDataProvider
    {
        MySqlCommand _cmd;


        public int LayMaHoaDonMax()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select max(MaHD) from HoaDon";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            String str = this.ExecuteScalar(_cmd);

            if (str==null || str == "")
            {
                return 0;
            }
            
            return int.Parse(str);
        }

        public List<HoaDonDTO> LayDanhSachHoaDon()     
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from HoaDon";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<HoaDonDTO> list = new List<HoaDonDTO>();

            foreach (DataRow row in dt.Rows)
            {
                HoaDonDTO  dto = new HoaDonDTO();
                dto.MaHoaDon = int.Parse(row["MaHD"].ToString());
                dto.TenKH = row["TenKH"].ToString();
                dto.DienThoai = row["DienThoai"].ToString();
                dto.Email = row["Email"].ToString();             
                dto.NgayGioDatHang = DateTime.Parse(row["NgayGioDatHang"].ToString());
                dto.DiaChiNhan = row["DiaChiNhan"].ToString();
                dto.NgayGioGiaoHang = DateTime.Parse(row["NgayGioGiaoHang"].ToString());
                dto.TongTien = double.Parse(row["TongTien"].ToString());
                dto.MaTinhTrangHD = int.Parse(row["MaTinhTrangHD"].ToString());
                dto.MaTaiKhoan = row["MaTaiKhoan"].ToString();
                dto.HinhThucThanhToan = row["HinhThucThanhToan"].ToString();
             
                list.Add(dto);
            }

            return list;
        }
        public HoaDonDTO LayHoaDon(int MaHoaDon)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from HoaDon where MaHD = @MaHD";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaHD", MaHoaDon.ToString()));


            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            HoaDonDTO ct = new HoaDonDTO();

            try
            {

                DataRow row = dt.Rows[0];//kết quả trả ra ở dòng 0

                ct.MaHoaDon = int.Parse(row["MaHD"].ToString());
                ct.TenKH = row["TenKH"].ToString();
                ct.DienThoai = row["DienThoai"].ToString();
                ct.Email = row["Email"].ToString();
                ct.NgayGioDatHang = DateTime.Parse(row["NgayGioDatHang"].ToString());
                ct.DiaChiNhan = row["DiaChiNhan"].ToString();
                ct.NgayGioGiaoHang = DateTime.Parse(row["NgayGioGiaoHang"].ToString());
                ct.TongTien = double.Parse(row["TongTien"].ToString());
                ct.MaTinhTrangHD = int.Parse(row["MaTinhTrangHD"].ToString());
                ct.MaTaiKhoan = row["MaTaiKhoan"].ToString();
                ct.HinhThucThanhToan = row["HinhThucThanhToan"].ToString();

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ct;
        }
        public Boolean ThemHoaDon(HoaDonDTO ct)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into HoaDon(TenKH,DienThoai,Email,NgayGioDatHang,DiaChiNhan,NgayGioGiaoHang,TongTien,MaTinhTrangHD,MaTaiKhoan,HinhThucThanhToan) values(@TenKH,@DienThoai,@Email,@NgayGioDatHang,@DiaChiNhan,@NgayGioGiaoHang,@TongTien,@MaTinhTrangHD,@MaTaiKhoan,@HinhThucThanhToan)";
            _cmd.CommandType = CommandType.Text;

            _cmd.Parameters.Add(new MySqlParameter("@TenKH", ct.TenKH));
            _cmd.Parameters.Add(new MySqlParameter("@DienThoai", ct.DienThoai));
            _cmd.Parameters.Add(new MySqlParameter("@Email", ct.Email));
            _cmd.Parameters.Add(new MySqlParameter("@NgayGioDatHang", ct.NgayGioDatHang));
            _cmd.Parameters.Add(new MySqlParameter("@DiaChiNhan", ct.DiaChiNhan));
            _cmd.Parameters.Add(new MySqlParameter("@NgayGioGiaoHang", ct.NgayGioGiaoHang));
            _cmd.Parameters.Add(new MySqlParameter("@TongTien", ct.TongTien));
            _cmd.Parameters.Add(new MySqlParameter("@MaTinhTrangHD", ct.MaTinhTrangHD));
            _cmd.Parameters.Add(new MySqlParameter("@MaTaiKhoan", ct.MaTaiKhoan));
            _cmd.Parameters.Add(new MySqlParameter("@HinhThucThanhToan", ct.HinhThucThanhToan));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean CapNhatHoaDon(HoaDonDTO ct)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update HoaDon set TenKH = @TenKH , DienThoai = @DienThoai , Email = @Email , NgayGioDatHang = @NgayGioDatHang , DiaChiNhan = @DiaChiNhan , NgayGioGiaoHang = @NgayGioGiaoHang , TongTien = @TongTien , MaTinhTrangHD = @MaTinhTrangHD , MaTaiKhoan = @MaTaiKhoan , HinhThucThanhToan = @HinhThucThanhToan where MaHD = @MaHD";
            _cmd.CommandType = CommandType.Text;

            _cmd.Parameters.Add(new MySqlParameter("@TenKH", ct.TenKH));
            _cmd.Parameters.Add(new MySqlParameter("@DienThoai", ct.DienThoai));
            _cmd.Parameters.Add(new MySqlParameter("@Email", ct.Email));
            _cmd.Parameters.Add(new MySqlParameter("@NgayGioDatHang", ct.NgayGioDatHang));
            _cmd.Parameters.Add(new MySqlParameter("@DiaChiNhan", ct.DiaChiNhan));
            _cmd.Parameters.Add(new MySqlParameter("@NgayGioGiaoHang", ct.NgayGioGiaoHang));
            _cmd.Parameters.Add(new MySqlParameter("@TongTien", ct.TongTien));
            _cmd.Parameters.Add(new MySqlParameter("@MaTinhTrangHD", ct.MaTinhTrangHD));
            _cmd.Parameters.Add(new MySqlParameter("@MaTaiKhoan", ct.MaTaiKhoan));
            _cmd.Parameters.Add(new MySqlParameter("@HinhThucThanhToan", ct.HinhThucThanhToan));

            _cmd.Parameters.Add(new MySqlParameter("@MaHD", ct.MaHoaDon));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean XoaHoaDon(int MaHoaDon)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from HoaDon where MaHD = @MaHD";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaHD", MaHoaDon.ToString()));

            return ExecuteNonQuery(_cmd);
        }
     
    }
}
